package com.wangyadong.hobby.schedule.jobtask.thread;

import java.util.concurrent.TimeUnit;

public class VolatileTest5 {
    private static  int INIT_VALUE = 0;
    private static int MAX = 100;

    public static void main(String[] args) {
        test();
    }

    public static void test() {
        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (localValue < MAX) {
                if (INIT_VALUE != localValue) {
                    System.out.printf("the reader value is [%d]\n", INIT_VALUE);
                    localValue = INIT_VALUE;
                }
            }
        }, "reader").start();
        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (INIT_VALUE < MAX) {
                System.out.printf("the update value s=is [%d]\n", ++localValue);
                INIT_VALUE = localValue;
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }, "update").start();
    }
}
